//
//  STSearchBar.swift
//  Story
//
//  Created by 汤泽川 on 2019/4/18.
//  Copyright © 2019 汤泽川. All rights reserved.
//

import Foundation
import UIKit
import SnapKit
import RxSwift
import RxCocoa

class STSearchBar: UIView, UITextFieldDelegate {
    
    public let searchingObserver = PublishSubject<Bool>()
    public let searchBtnObserver = PublishSubject<Void>()
    
    private let disposeBag = DisposeBag()
    
    private let cancelBtn : UIButton = {
        let btn = UIButton()
        btn.setImage(#imageLiteral(resourceName: "story_back"), for: .normal)
        return btn
    }()
    
    private let contentView : UIView = {
        let view = UIView()
        view.backgroundColor = #colorLiteral(red: 0.9204053283, green: 0.9204053283, blue: 0.9204053283, alpha: 1)
        view.layer.cornerRadius = 6
        return view
    }()
    
    private let imageView : UIView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "search")
        return imageView
    }()
    
    public let textField : UITextField = {
        let textField = UITextField()
        textField.placeholder = "搜索更多精彩故事"
        textField.textColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)
        textField.textAlignment = NSTextAlignment.left
        textField.font = UIFont.systemFont(ofSize: 15)
        textField.returnKeyType = .search
        return textField
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        buildView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func buildView() {
        self.addSubview(cancelBtn)
        cancelBtn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(4)
            make.centerY.equalToSuperview()
            make.size.equalTo(CGSize(width: 22, height: 22))
        }
        cancelBtn.rx.tap.subscribe(onNext: { [weak self] in
            self?.textField.resignFirstResponder()
            self?.searchingObserver.onNext(false)
            self?.textField.text = nil
            UIView.animate(withDuration: 0.25, animations: {
                self?.contentView.snp.remakeConstraints({ (make) in
                    make.top.bottom.right.left.equalToSuperview()
                })
                self?.contentView.setNeedsLayout()
                self?.contentView.layoutIfNeeded()
            })
        }).disposed(by: disposeBag)
        
        self.addSubview(contentView)
        contentView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        contentView.addSubview(imageView)
        imageView.snp.updateConstraints { (make) in
            make.top.left.equalToSuperview().offset(8)
            make.bottom.equalToSuperview().offset(-8)
            make.width.equalTo(imageView.snp.height)
        }
        contentView.addSubview(textField)
        textField.snp.updateConstraints { (make) in
            make.left.equalTo(imageView.snp_right).offset(14)
            make.centerY.right.equalToSuperview()
        }

        textField.delegate = self
        textField.rx.controlEvent(.editingDidBegin).subscribe(onNext: { [weak self] in
            UIView.animate(withDuration: 0.25, animations: {
                self?.contentView.snp.remakeConstraints({ (make) in
                    make.left.equalTo(self!.cancelBtn.snp_right).offset(8)
                    make.top.bottom.right.equalToSuperview()
                })
                self?.contentView.setNeedsLayout()
                self?.contentView.layoutIfNeeded()
            })
            self?.searchingObserver.onNext(true)
        }).disposed(by: disposeBag)
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if textField.text != nil && textField.text != "" {
            self.searchBtnObserver.onNext(())
        }
        return true
    }
}
